package com.ygqh.baby.service.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.TaskMethod;
import com.ygqh.baby.model.ngmm.NgmmOrder;
import com.ygqh.baby.model.ngmm.NgmmOrderInfoResult;
import com.ygqh.baby.model.ngmm.NgmmOrderListResult;
import com.ygqh.baby.model.ngmm.NgmmOrderStatus;
import com.ygqh.baby.service.YgNgmmService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgTaskTimeService;
import com.ygqh.baby.service.YgTaskTimeTemplate;
import com.ygqh.baby.service.ngmm.api.NgmmApi;
import com.ygqh.baby.service.task.YgNgmmTaskService;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.ExceptionUtil;
import com.ygqh.baby.utils.MailUtil;

@Service
public class YgNgmmTaskServiceImpl implements YgNgmmTaskService {
	protected final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private YgTaskTimeService ygTaskTimeService;
	@Autowired
	private NgmmApi ngmmApi;
	@Autowired
	private YgNgmmService ygNgmmService;
	@Autowired
	private YgOrderService ygOrderService;
	@Autowired
	private MailUtil mailUtil;

	@Override
	public void loadNgmmOrder(int timeInterval) {
		ygTaskTimeService.execute(TaskMethod.LoadNgmmOrder, timeInterval, new YgTaskTimeTemplate() {

			@Override
			public void execute(Date runTime) throws Exception {
				Date startTime = DateConvertUtils.addMinute(runTime, -(timeInterval + 2));
				Date endTime = DateConvertUtils.addMinute(runTime, -2);

				String orderStatus = NgmmOrderStatus.NotifiedDistribution.getCode().toString();
				Integer pageNo = 1;
				Integer pageSize = 100;
				NgmmOrderListResult result = ngmmApi.orderQueryList(startTime, endTime, pageNo, pageSize, orderStatus, "modified");
				handleOrderQueryResult(result);
				while (result != null && result.getData().getHasNext()) {
					pageNo++;
					result = ngmmApi.orderQueryList(startTime, endTime, pageNo, pageSize, orderStatus, "modified");
					handleOrderQueryResult(result);
				}
			}

			private void handleOrderQueryResult(NgmmOrderListResult result) {
				if (result != null && CollectionUtils.isNotEmpty(result.getData().getOrders())) {
					List<NgmmOrder> orders = result.getData().getOrders();
					// 去重
					List<Long> orderIds = orders.stream().map(o -> o.getOrderId()).collect(Collectors.toList());
					List<Long> exitOrderIds = ygOrderService.findExitOrderIds(orderIds);
					orderIds.removeAll(exitOrderIds);
					List<NgmmOrder> resultOrders = new ArrayList<>();
					for (Long orderId : orderIds) {
						try {
							NgmmOrderInfoResult infoResult = ngmmApi.orderQueryInfo(orderId.toString());
							if (infoResult != null) {
								resultOrders.addAll(infoResult.getOrders());
							}
						} catch (Exception e) {
							e.printStackTrace();
							logger.error("年糕妈妈订单详情查询错误。ngmm_order_no=" + orderId + ExceptionUtil.getExceptionMsg(e));
						}
					}
					try {
						ygNgmmService.saveNgmmOrders(resultOrders);
					} catch (Exception e) {
						logger.error("年糕妈妈订单保存错误！" + ExceptionUtil.getExceptionMsg(e));
						try {
							mailUtil.sendWithHtml(new String[]{"sunshuo@yiigoo.cn"}, "年糕妈妈订单", "orderIds=" + orderIds.toString() + ", error=" +ExceptionUtil.getExceptionMsg(e));
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});
	}

}
